﻿using System;
using System.IO;
using System.Linq;
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Util;
using Com.Appbrain;
using HalalCheck.DataModels;
using HalalCheck.Utils;
using HalalCheck.View;
using SpotOffer.Utils;
using SQLite.Net;
using SQLite.Net.Platform.XamarinAndroid;
using Xamarin;
using Xamarin.Forms;
using Xamarin.Social;
using Xamarin.Social.Services;
using XLabs.Caching;
using XLabs.Caching.SQLite;
using XLabs.Forms;
using XLabs.Forms.Services;
using XLabs.Ioc;
using XLabs.Platform.Device;
using XLabs.Platform.Mvvm;
using XLabs.Platform.Services;
using XLabs.Platform.Services.Email;
using XLabs.Platform.Services.Media;
using XLabs.Serialization;
using XLabs.Serialization.ServiceStack;
// NOTE: Facebook SDK rquires that the 'Value' point to a string resource
//       in your values/ folder (eg: strings.xml file).
//       It will not allow you to use the app_id value directly here!
[assembly: MetaData("com.facebook.sdk.ApplicationId", Value = "@string/app_id")]
[assembly: Permission(Name = Android.Manifest.Permission.Internet)]
[assembly: Permission(Name = Android.Manifest.Permission.WriteExternalStorage)]
namespace HalalCheck.Droid
{
    [Activity(Label = "HalalCheck", Icon = "@drawable/icon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : XFormsApplicationDroid
    {
        /// <summary>
        /// Called when [create].
        /// </summary>
        /// <param name="bundle">The bundle.</param>
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
#if DEBUG
            Insights.Initialize("aff352677ed4c48c5200ad9491e6fd9a829cb237", this);
#else
            Insights.Initialize("04573c51cc5fcc1229c321005ddf96fef42b2555", this);
#endif
            if (!Resolver.IsSet)
            {
                this.SetIoc();
            }
            else
            {
                var app = Resolver.Resolve<IXFormsApp>() as IXFormsApp<XFormsApplicationDroid>;
                app.AppContext = this;
            }

            Forms.Init(this, bundle);

            App.Init();




            Forms.ViewInitialized += (sender, e) =>
            {
                if (!string.IsNullOrWhiteSpace(e.View.StyleId))
                {
                    e.NativeView.ContentDescription = e.View.StyleId;
                }
            };

            // this.SetPage(new App());
            LoadApplication(new App());
            AppBrain.Init(this);

        }

        /// <summary>
        /// Sets the IoC.
        /// </summary>
        private void SetIoc()
        {
            var resolverContainer = new SimpleContainer();

            var app = new XFormsAppDroid();

            app.Init(this);

            var documents = app.AppDataDirectory;
            var pathToDatabase = Path.Combine(documents, "halalcheck.db");


            resolverContainer.Register<IDevice>(t => AndroidDevice.CurrentDevice)
            .Register<IDisplay>(t => t.Resolve<IDevice>().Display)
            .Register<IFontManager>(t => new FontManager(t.Resolve<IDisplay>()))
                //  .Register<IJsonSerializer, Serialization.JsonNET.JsonSerializer>()
            .Register<IJsonSerializer, JsonSerializer>()
            .Register<IEmailService, EmailService>()
            .Register<IMediaPicker, MediaPicker>()
            .Register<ITextToSpeechService, TextToSpeechService>()
            .Register<IDependencyContainer>(resolverContainer)
            .Register<IXFormsApp>(app)
            .Register<INetwork,Network>()

            .Register<ISecureStorage>(t => new KeyVaultStorage(t.Resolve<IDevice>().Id.ToCharArray()))
            .Register<ISimpleCache>(
                t => new SQLiteSimpleCache(new SQLitePlatformAndroid(),
                    new SQLiteConnectionString(pathToDatabase, true), t.Resolve<IJsonSerializer>()));

            Resolver.SetResolver(resolverContainer.GetResolver());
        }

        public override void OnBackPressed()
        {
            base.OnBackPressed();
            if ((App.Root.CurrentPage.GetType() == (typeof (MenuPage))))
            {
                AppBrain.Ads.ShowInterstitial(this);
            }
        }


        /* public override void OnBackPressed()
        {
            if ((RootPage.Nav.CurrentPage.GetType() == (typeof(HalalListPage))) & !App.Root.IsPresented)
            {
                base.OnBackPressed();
                AppBrain.Ads.ShowInterstitial(this);
            }
            else if (App.Root.IsPresented)
            {
                App.Root.IsPresented = false;
            }
            else if (!App.Root.IsPresented & (RootPage.Nav.CurrentPage.GetType() != (typeof(HalalListPage))) & (RootPage.Nav.CurrentPage.GetType() != (typeof(HalalItemPage))))
            {
                RootPage._menu.MenuListView.SelectedItem = RootPage._menu.MenuListView.ItemsSource.Cast<MenuItemHalalCheck>().First();
            }
            else
            {
                base.OnBackPressed();
            }
        }*/
    }

}

